<?php
class JPush{
	public $_appkeys        = '63a2033764e12b2182fc0bac';
	public $_masterSecret   = '175712939d8ad4b48a300411';
	public $apns_production = false;

	/**
	 * 发送推送
	 * @param  string $title    标题
	 * @param  string $message  内容
	 * @param  string $audience 设备标识
	 * @param  string $platform 发送平台
	 */
    public function send($title,$message='',$audience='all',$extras='',$platform='all',$is_notification = true){
    	$message = $message?$message:$title;
		$url     = 'https://api.jpush.cn/v3/push';
		$base64  = base64_encode("$this->_appkeys:$this->_masterSecret");
		$header  = array("Authorization:Basic $base64","Content-Type:application/json");

		$data['platform'] = $platform?$platform:'all';// 平台，android、ios、all
		$data['audience'] = $audience?$audience:'all';// 推送设备指定

		//是否弹出推送
		if($is_notification == true){
			$data['notification']['alert'] = $title;//通知内容体
		}

		$data['message']['msg_content']  = $message;//消息内容体
		$data['message']['title']        = $title;//消息标题
		$data['message']['content_type'] = '';//消息内容类型
		$data['message']['extras']       = $extras;//消息扩展参数

		$data['options']['time_to_live']    = 60;
		$data['options']['apns_production'] = $this->apns_production;

		$data = json_encode($data);
		// var_dump($data);exit;
		$res     = $this->http($url,$data,$header);
		$res_arr = json_decode($res, true);
		// var_dump($res_arr);exit;
        return $res_arr;
    }
    public function http($url="",$param="",$header="") {
        if (empty($url) || empty($param)) {
        	return false;
        }
		$ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL, $url);//抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        // 增加 HTTP Header（头）里的字段 
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        // 终止从服务端进行验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		$data    = curl_exec($ch);//运行curl
		$getinfo = curl_getinfo($ch);
        if($getinfo['http_code']!=200){
           
        }
        curl_close($ch);
        return $data;
    }
    /**
     * 设置推送类型
     * @param string $type 类型，teacher推送给老师，student推送给学生
     */
    public function set_push_type($user_type='teacher'){
    	$jpush_config = C('jpush_config');
		//如果是老师端发送的，则使用学生端配置进行发送
		if($user_type == 'teacher'){
			$this->_appkeys        = $jpush_config['teacher']['app_keys'];
			$this->_masterSecret   = $jpush_config['teacher']['master_secret'];
			$this->apns_production = $jpush_config['teacher']['apns_production'];
		}else{
			$this->_appkeys        = $jpush_config['student']['app_keys'];
			$this->_masterSecret   = $jpush_config['student']['master_secret'];
			$this->apns_production = $jpush_config['student']['apns_production'];
		}
    }
}
?>